package com.example.myyiyanmengjava;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.mvplibrary.ui.activity.BaseActivity;
import com.example.myyiyanmengjava.adapter.FragVpAdapter;
import com.example.myyiyanmengjava.bean.Person;
import com.example.myyiyanmengjava.bean.SheetBean;
import com.example.myyiyanmengjava.fragment.BackHandlerHelper;
import com.example.myyiyanmengjava.fragment.SheetFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.ResourceSubscriber;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class SheetActivity extends BaseActivity {
    private static String TAG = "朱勇闯activ";

    private HashMap<String, String> buFenMap;
    private HashMap<String, String> headMap;
    private String token;
    private String ti_id;
    private String ban_ben;
    private static int size;
    private ViewPager vp_sheet;
    private TextView back;
    private List<SheetBean.InfoBean.ListBean> listBeans;
    private FragVpAdapter fragVpAdapter;
    private List<Fragment> fragments;

    @Override
    protected void initData() {
        listBeans = new ArrayList<>();
        vp_sheet = findViewById(R.id.vp_sheet);
        back = findViewById(R.id.back_sheet);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

//        vp_sheet.setAdapter(fragVpAdapter);
        Intent intent = getIntent();
        token = intent.getStringExtra("token");
        ti_id = intent.getStringExtra("ti_id");
        ban_ben = intent.getStringExtra("ban_ben");
        Log.e(TAG, "token:" + token);
        Log.e(TAG, "ban_ben:" + ban_ben);
        headMap = new HashMap<>();
        if (token != null) {
            headMap.put("A-uthorization", token);
            initBuFen();
        } else {
            Toast.makeText(this, "token为空", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected int createLayoutId() {
        return R.layout.activity_sheet;
    }

    private void initBuFen() {
        buFenMap = new HashMap<>();
        buFenMap.put("ti_id", ti_id);
        buFenMap.put("ban_ben", ban_ben);
        new Retrofit.Builder()
                .baseUrl(ApiService.baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build()
                .create(ApiService.class)
                .getInterList(buFenMap, headMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ResourceSubscriber<SheetBean>() {
                    @Override
                    public void onNext(SheetBean sheetBean) {
                        listBeans = sheetBean.getInfo().getList();
//                        Log.e(TAG, "onNext: " + listBeans.get(0).getName());
                        Toast.makeText(SheetActivity.this, listBeans.get(0).getName(), Toast.LENGTH_SHORT).show();

                    }

                    @Override
                    public void onError(Throwable t) {
                        Log.e(TAG, "onError: " + t.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        fragments = new ArrayList<>();
                        for (int i = 0; i < listBeans.size(); i++) {
                            ArrayList<String> strings = new ArrayList<>();
                            String name = listBeans.get(i).getName();
                            String xiang_jie = listBeans.get(i).getXiang_jie();
                            String da_an = listBeans.get(i).getDa_an();
                            String ti_type = listBeans.get(i).getTi_type();
                            String A = listBeans.get(i).getData().getA();
                            String B = listBeans.get(i).getData().getB();
                            String C = listBeans.get(i).getData().getC();
                            String D = listBeans.get(i).getData().getD();
                            Log.e(TAG, "onComplete: " + listBeans.get(i).getData().getE());
                            if (listBeans.get(i).getData().getE() != null) {
                                Log.e(TAG, "onComplete:222222222222 " + listBeans.get(i).getData().getE());
                                String E = listBeans.get(i).getData().getE();
                                strings.add(A);
                                strings.add(B);
                                strings.add(C);
                                strings.add(D);
                                strings.add(E);
                            } else {
                                strings.add(A);
                                strings.add(B);
                                strings.add(C);
                                strings.add(D);
                            }

                            Person person = new Person(name, da_an, xiang_jie, ti_type, strings);
                            person.setSelected(true);
                            SheetFragment sheetFragment = new SheetFragment();
                            Bundle bundle = new Bundle();
                            bundle.putSerializable("person", person);
                            sheetFragment.setArguments(bundle);
                            fragments.add(sheetFragment);
                        }
                        Log.e(TAG, "onNext: " + fragments.size());
//                        Log.e(TAG, "onNext: " + fragments.size());
                        fragVpAdapter = new FragVpAdapter(getSupportFragmentManager(), fragments);
                        vp_sheet.setAdapter(fragVpAdapter);
                    }
                });
    }

    /*  @Override
      public boolean onKeyDown(int keyCode, KeyEvent event) {
          if (keyCode == KeyEvent.KEYCODE_BACK) {
              Fragment ft = (Fragment) getSupportFragmentManager().findFragmentById(R.id.realtabcontent);//realtabcontent为你的容器，装载fragment的
              if (ft instanceof HomeFragment || ft instanceof PersonalFragment) {
                  if ((System.currentTimeMillis() - mExitTime) > 2000) {
                      Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                      mExitTime = System.currentTimeMillis();
                  } else {
                      finish();
                  }
                  return true;
              } else if (ft instanceof LoginFragment) {
                  ((LoginFragment) ft).onKeyDown(keyCode, event);//onKeyDown在LoginFragment里面重写了，只是随便写了一个方法而已.......
                  return false;
              } else if (ft instanceof RegisterFragment) {
                  ((RegisterFragment) ft).onKeyDown(keyCode, event);
                  return false;
              }


          }
          return super.onKeyDown(keyCode, event);
      }*/


    @Override
    public void onBackPressed() {
        if (!BackHandlerHelper.handleBackPress(this)) {
            super.onBackPressed();
        }
    }
}